package com.myyule.filesystem.common;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.mortennobel.imagescaling.ResampleOp;

public class ImageUtil {
	/**
	 * 截图图片大小
	 * 
	 * @param buffer 图片字节数组
	 * @param width  图片宽度
	 * @param height 图片高度
	 * @param format 图片格式
	 * @return
	 */
	public static byte[] resizeImage(byte[] buffer, Integer width,
			Integer height, String format) {
	    if(width.equals(0)||height.equals(0)){
	        return null;
	    }
		try {
		    Integer relWhidth = 0;
		    Integer relHight = 0;
			InputStream input = new ByteArrayInputStream(buffer);
			BufferedImage inputBufImage = ImageIO.read(input);
			Integer imgWidth = inputBufImage.getWidth();
			Integer imgHight = inputBufImage.getHeight();
			if(imgWidth.intValue()<3||imgHight<3){
			    return null;
			}
			/* 如果结果的宽高都大于原图，结果就是原图大小*/
			if(imgWidth.intValue()<=width.intValue()&&imgHight.intValue()<=height.intValue()){
			    relWhidth = imgWidth;
			    relHight = imgHight;
			 /* 如果结果的宽高都小于原图*/
			}else if(imgWidth.intValue()>width.intValue()&&imgHight.intValue()>height.intValue()){
				/* 如果原图的宽与结果的宽之比小于原图的高与结果的高之比，即高的比例更小，时以结果的高为高，等比计算出宽*/
			    if(imgWidth.floatValue()/width.floatValue()<=imgHight.floatValue()/height.floatValue()){
			        relHight = height;
			        relWhidth = (int) (imgWidth.floatValue()*height.floatValue()/imgHight.floatValue());
			        /* 反之计算出高*/
			    }else{
			        relWhidth = width;
			        relHight = (int)(imgHight.floatValue()*width.floatValue()/imgWidth.floatValue());
			    }
			 /* 当结果的宽小于原图的宽，高大于原图的高时，已结果的宽为宽，等比计算出结果的高*/   
			}else if(imgWidth.intValue()>width&&imgHight.intValue()<height.intValue()){
			    relWhidth = width;
                relHight = (int)(imgHight.floatValue()*width.floatValue()/imgWidth.floatValue());
             /* 当结果的宽大于原图的宽，高小于原图的高时，已结果的高为高，等比计算出结果的宽*/   			    
			}else if(imgWidth.intValue()<width&&imgHight.intValue()>height.intValue()){
			    relHight = height;
                relWhidth = (int)(imgWidth.floatValue()*height.floatValue()/imgHight.floatValue());
			}
			if(relHight.intValue()<3){
			    relHight = 3;
			    relWhidth =  (int)(imgWidth.floatValue()*height.floatValue()/3);
			    relWhidth =  relWhidth.intValue()>width?width:relWhidth;
			}
			if(relWhidth.intValue()<3){
			    relWhidth = 3;		   
			    relHight = (int)(imgHight.floatValue()*width.floatValue()/3);
			    relHight = relHight.intValue()>height.intValue()?height:relHight;			 
			}
			ResampleOp resampleOp = new ResampleOp(relWhidth, relHight);
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			try {
				ImageIO.write(rescaledTomato, format, out);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 按比例截取图片
	 * 
	 * @param buffer
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public static byte[] resizeImageWithRatio(byte[] buffer, Integer width, Integer height, String format) {
		try {
			InputStream input = new ByteArrayInputStream(buffer);
			BufferedImage inputBufImage = ImageIO.read(input);
			ResampleOp resampleOp = null;
			int reHeight = 0;
			int reWidth = 0;
			
			if (height > inputBufImage.getHeight() || width > inputBufImage.getWidth()) {
				return buffer;
			}
			
			if ((width.floatValue() / height.floatValue()) < (((Integer) inputBufImage.getWidth()).floatValue() 
					/ ((Integer) inputBufImage.getHeight()).floatValue())) {
				reHeight = inputBufImage.getHeight() * width / inputBufImage.getWidth();
				reWidth = width;
			} else {
				reWidth = inputBufImage.getWidth() * height / inputBufImage.getHeight();
				reHeight = height;
			}
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			resampleOp = new ResampleOp(reWidth, reHeight);
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
			ImageIO.write(rescaledTomato, format, out);
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte[] resizeImageWithRatio(byte[] buffer, Integer width, String format) {
		try {
			InputStream input = new ByteArrayInputStream(buffer);
			BufferedImage inputBufImage = ImageIO.read(input);
			ResampleOp resampleOp = null;
			int reHeight = 0;
			int reWidth = 0;
			
			if (width > inputBufImage.getWidth()) {
				return buffer;
			}
			reHeight = inputBufImage.getHeight() * width/ inputBufImage.getWidth();
			reWidth = width;
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			resampleOp = new ResampleOp(reWidth, reHeight);
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
			ImageIO.write(rescaledTomato, format, out);
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
